package com.vonchange.common.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.vonchange.utils.config.SystemConfig;
import com.vonchange.utils.web.RequestUtils;
import com.vonchange.utils.web.ResponseUtils;

/**
 * 
 * 需要登录验证的拦截器 和视图中需要的必要常量
 * 
 * @author von_change@163.com
 * @date 2016年4月20日 下午1:30:07
 * @since 1.0
 */
public class CommonInterceptor implements HandlerInterceptor {
	//private static Log logger = LogFactory.getLog(CommonInterceptor.class);

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if(RequestUtils.getLocation(request).indexOf("undefined")!=-1){
			ResponseUtils.sendNotFound(response);
			return false;
		}
		if(null==request.getHeader("user-agent")){
			return true;
		}
		if (!HandlerMethod.class.isAssignableFrom(handler.getClass())) {
			return true;
		}
		return true;
	}


	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		if (null != modelAndView) {
			if(modelAndView.getViewName().indexOf(":")==-1){
				modelAndView.addObject("base", request.getContextPath());
				modelAndView.addObject("now", new Date());
				modelAndView.addAllObjects(SystemConfig.sysConfigToModelMap);
			}
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		// TODO Auto-generated method stub

	}

}
